/***************************************************************************
 *   Copyright (C) 2007 by Ivan Vasi?                                      *
 *   ivasic@gmail.com                                                      *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
 ***************************************************************************/
#include "settingswidget.h"

#include "globals.h"
#include "trackerserver.h"

#include <QtGui>

SettingsWidget::SettingsWidget(QWidget* parent, Qt::WFlags fl)
		: QWidget(parent, fl), Ui::Settings()
{
	setupUi(this);
	update();
	
	m_serverPort = intPort->value();
}

SettingsWidget::~SettingsWidget()
{}

/*$SPECIALIZATION$*/

void SettingsWidget::apply()
{
	qDebug() << "Applying settings";
	bool portChanged = m_serverPort != intPort->value();
	
	if(portChanged)
	{
		QMessageBox::warning(this, tr("Restarting server!"), tr("Server needs to be restarted to change TCP port"), QMessageBox::Ok);
		
		Globals::instance().server()->stop();
	}
	
	Globals::instance().settings()->setValue("server_port", intPort->value());
	Globals::instance().settings()->setValue("connection_timeout", intTimeout->value());
	Globals::instance().settings()->setValue("max_connections", intConnections->value());
	
	Globals::instance().server()->setMax_connections(intConnections->value());
	
	if(portChanged)
	{		
		Globals::instance().server()->start();
	}
	
	//db
	Globals::instance().settings()->setValue("db_host", txtDBHost->text());
	Globals::instance().settings()->setValue("db_name", txtDBName->text());
	Globals::instance().settings()->setValue("db_username", txtDBUsername->text());
	Globals::instance().settings()->setValue("db_password", txtDBPassword->text());
	Globals::instance().settings()->setValue("db_port", intDBPort->value());
}

void SettingsWidget::update()
{	
	intPort->setValue(Globals::instance().settings()->value("server_port", 23982).toUInt());
	intTimeout->setValue(Globals::instance().settings()->value("connection_timeout", 5000).toUInt());
	intConnections->setValue(Globals::instance().settings()->value("max_connections", 0).toUInt());
	
	
	intDBPort->setValue(Globals::instance().settings()->value("db_port", 3306).toInt());
	txtDBHost->setText(Globals::instance().settings()->value("db_host", "localhost").toString());
	txtDBName->setText(Globals::instance().settings()->value("db_name", "qtracker").toString());
	txtDBUsername->setText(Globals::instance().settings()->value("db_username", "root").toString());
	txtDBPassword->setText(Globals::instance().settings()->value("db_password", "").toString());
}

void SettingsWidget::showApplyButton(bool show)
{
	if(show)
		btnApply->show();
	else
		btnApply->hide();
}
